package com.sgb.kjwl.view.widget;

import android.content.Context;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.link.entity.BrandEntity;
import com.swgk.core.util.ACAdminDialogUtils;
import com.sgb.kjwl.view.ui.activity.self.publish.AddBrandActivity;
import com.sgb.kjwl.view.ui.adapter.self.publish.BrandAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

public class BrandPopWindow extends BasePopWindow {

    private RecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private BrandAdapter brandAdapter;
    private List<BrandEntity> brandEntityList;
    private LinearLayout noDataLay;
    private String brandId = "";
    private String brandName = "";
    private LinearLayout linearLayout;
    private EditText editText;
    private InputTextInterface inputTextInterface;
    //商品分类ID
    private String typeId;
    //是否重新刷新数据
    private boolean isUpdate;
    private boolean isShowSearch = false;

    public BrandPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    public BrandPopWindow(Context context, boolean isShowSearch) {
        super(context, DIR_DOWN_UP);
        this.isShowSearch = isShowSearch;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_brand;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.skill_list);
        okView = holder.getPop().findViewById(R.id.ok_view);
        editText = holder.getPop().findViewById(R.id.et_view);
        noDataLay = holder.getPop().findViewById(R.id.ll_empty);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        linearLayout = holder.getPop().findViewById(R.id.ll_add);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(brandId, brandName, null);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.IS_DATA, "").contains("3_pinpaiguanli")) {
                    AddBrandActivity.start(mContext, typeId);
                } else {
                    ACAdminDialogUtils.showACDialog(ACAdminDialogUtils.TYPE_NO_PERMISSION, mContext);
                }
            }
        });
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    inputTextInterface.input(editText.getText().toString().trim());
                }
                return false;
            }
        });
        brandAdapter = new BrandAdapter(mContext, null);
        brandAdapter.setItemSelect(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(brandAdapter);
        brandAdapter.iSelectChild(new BrandAdapter.ISelectChild() {
            @Override
            public void select(String name, String id) {
                brandName = name;
                brandId = id;
            }
        });
    }

    public void setData(List<BrandEntity> data, String typeId, boolean isUpdate) {
        this.brandEntityList = data;
        this.typeId = typeId;
        this.isUpdate = isUpdate;
        brandAdapter.setDatas(brandEntityList);
        if (isUpdate) {
            brandAdapter.setDefItem(-1);
        }
        brandAdapter.IsUpdate(isUpdate);
        if (!isHaveBrand(data)) {
            noDataLay.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            noDataLay.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
        if (!isShowSearch) {
            linearLayout.setVisibility(View.GONE);
            editText.setVisibility(View.GONE);
        }
    }

    private boolean isHaveBrand(List<BrandEntity> data) {
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getChildList() != null && data.get(i).getChildList().size() > 0) {
                return true;
            }
        }
        return false;
    }

    public interface InputTextInterface {
        void input(String s);
    }

    public void inputTextInterface(InputTextInterface inputTextInterface) {
        this.inputTextInterface = inputTextInterface;
    }

}
